package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.model.GoodsType;
import com.zimmur.app.mocolink.domain.vo.GoodsCategoryVo;
import com.zimmur.app.mocolink.domain.vo.GoodsTypeVo;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IGoodsCategoryService;

@Controller
@RequestMapping("/goodsType")
public class GoodsTypeController extends BaseController {

	@Autowired
	private IGoodsCategoryService goodsCategoryService;
	@RequestMapping("/index")
	public String index(HttpServletRequest request,ModelMap model){
		List<GoodsTypeVo> list = goodsCategoryService.selectAll();
		model.addAttribute("typeList", list);
		return "/center/goodsType/list";
	}
	@RequestMapping("/edit")
	public String edit(HttpServletRequest request,ModelMap model){
		Integer typeId = getParam("typeId", 0);
		GoodsType goodsType = goodsCategoryService.selectById(typeId);
		List<GoodsTypeVo> list = goodsCategoryService.selectAll();
		model.addAttribute("typeList", list);
		model.addAttribute("goodsType", goodsType);
		return "/center/goodsType/edit";
	}
	@RequestMapping("/add")
	public String add(HttpServletRequest request,ModelMap model){
		List<GoodsTypeVo> list = goodsCategoryService.selectAll();
		model.addAttribute("typeList", list);
		model.addAttribute("goodsType", new GoodsType());
		return "/center/goodsType/edit";
	}
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(GoodsType goodsType){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		int flag = 0;
		if(goodsType.getId()!=null && goodsType.getId()>0){
			flag = goodsCategoryService.update(goodsType);
		}else{
			flag = goodsCategoryService.add(goodsType);
		}
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;	
		}
		
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer typeId = getParam("typeId",0);
		int flag = goodsCategoryService.delete(typeId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/updateState")
	public ReturnJsonEntity updateState(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer typeId = getParam("typeId",0);
	
		int flag = goodsCategoryService.updateStatus(typeId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE320401;
		}
		entity.init(codeEnum);
		return entity;
	}
}
